/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sjtu.servlet;

import com.sjtu.bean.BookBean;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author p w
 */
public class SearchBookServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        this.doWork(request, response);
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doWork(request, response);
    }

    public void doWork(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("Here");
        String obj = request.getParameter("obj");
        JSONObject jsonObj = null;
        try {
            jsonObj = new JSONObject(obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
        PrintWriter writer = response.getWriter();
        try {
            String searchType = jsonObj.getString("searchType");
            String KeyWord = jsonObj.getString("KeyWord");
            JSONObject jsonRes = new JSONObject();
            ArrayList<String> bookName = new ArrayList<String>();
            ArrayList<String> author = new ArrayList<String>();
            ArrayList<String> publisher = new ArrayList<String>();
            ArrayList<String> url = new ArrayList<String>();
            ArrayList<String> id = new ArrayList<String>();
            ArrayList<Integer> score = new ArrayList<Integer>();
            if (searchType.equals("Book Name")) {
                BookBean book = BookBean.getByName(KeyWord);
                bookName.add(book.getName());
                author.add(book.getAuthor());
                publisher.add(book.getPublisher());
                url.add(book.getCoverURL());
                id.add(book.getId() + "");
                score.add(book.getScore());
            }
            if (searchType.equals("Book ISBN")) {
                BookBean book = BookBean.getByISBN(KeyWord);
                bookName.add(book.getName());
                author.add(book.getAuthor());
                publisher.add(book.getPublisher());
                url.add(book.getCoverURL());
                id.add(book.getId() + "");
                score.add(book.getScore());
            }
            if (searchType.equals("Publisher")) {
                ArrayList<BookBean> books = BookBean.getByPublisher(KeyWord);
                for (int i = 0; i < books.size(); i++) {
                    bookName.add(books.get(i).getName());
                    author.add(books.get(i).getAuthor());
                    publisher.add(books.get(i).getPublisher());
                    url.add(books.get(i).getCoverURL());
                    id.add(books.get(i).getId() + "");
                    score.add(books.get(i).getScore());
                }
            }
            if (searchType.equals("Book Author")) {
                ArrayList<BookBean> books = BookBean.getByAuthor(KeyWord);
                for (int i = 0; i < books.size(); i++) {
                    bookName.add(books.get(i).getName());
                    author.add(books.get(i).getAuthor());
                    publisher.add(books.get(i).getPublisher());
                    url.add(books.get(i).getCoverURL());
                    id.add(books.get(i).getId() + "");
                    score.add(books.get(i).getScore());
                }
            }
            if (searchType.equals("key words")) {
                ArrayList<BookBean> books = BookBean.getByLabel(KeyWord);
                System.out.println("new new " + books.size());
                for (int i = 0; i < books.size(); i++) {
                    bookName.add(books.get(i).getName());
                    author.add(books.get(i).getAuthor());
                    publisher.add(books.get(i).getPublisher());
                    url.add(books.get(i).getCoverURL());
                    id.add(books.get(i).getId() + "");
                    score.add(books.get(i).getScore());
                }
            }
            jsonRes.put("bookNameList", bookName);
            jsonRes.put("authorList", author);
            jsonRes.put("publisherList", publisher);
            jsonRes.put("urlList", url);
            jsonRes.put("idList", id);
            jsonRes.put("scoreList", score);
            System.out.println(jsonRes.toString());
            writer.write(jsonRes.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
